*/
class CssContent extends TextContent {
+ /**
+ * @var bool|Title|null
+ */
+ private $redirectTarget = false;
+
/**
* @param string $text CSS code.
* @param string $modelId the content content model
return $html;
}
+ /**
+ * @param Title $target
+ * @return CssContent
+ */
+ public function updateRedirect( Title $target ) {
+ if ( !$this->isRedirect() ) {
+ return $this;
+ }
+
+ return $this->getContentHandler()->makeRedirectContent( $target );
+ }
+
+ /**
+ * @return Title|null
+ */
+ public function getRedirectTarget() {
+ if ( $this->redirectTarget !== false ) {
+ return $this->redirectTarget;
+ }
+ $this->redirectTarget = null;
+ $text = $this->getNativeData();
+ if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
+ // Extract the title from the url
+ preg_match( '/title=(.*?)&action=raw/', $text, $matches );
+ if ( isset( $matches[1] ) ) {
+ $title = Title::newFromText( $matches[1] );
+ if ( $title ) {
+ // Have a title, check that the current content equals what
+ // the redirect content should be
+ if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
+ $this->redirectTarget = $title;
+ }
+ }
+ }
+ }
+
+ return $this->redirectTarget;
+ }
+
}
protected function getContentClass() {
return 'CssContent';
}
+
+ public function supportsRedirects() {
+ return true;
+ }
+
+ /**
+ * Create a redirect that is also valid CSS
+ *
+ * @param Title $destination
+ * @param string $text ignored
+ * @return JavaScriptContent
+ */
+ public function makeRedirectContent( Title $destination, $text = '' ) {
+ // The parameters are passed as a string so the / is not url-encoded by wfArrayToCgi
+ $url = $destination->getFullURL( 'action=raw&ctype=text/css', false, PROTO_RELATIVE );
+ $class = $this->getContentClass();
+ return new $class( '/* #REDIRECT */@import ' . CSSMin::buildUrlValue( $url ) . ';' );
+ }
+
}
--- /dev/null
+<?php
+
+class CssContentHandlerTest extends MediaWikiTestCase {
+
+ /**
+ * @dataProvider provideMakeRedirectContent
+ * @covers CssContentHandler::makeRedirectContent
+ */
+ public function testMakeRedirectContent( $title, $expected ) {
+ $this->setMwGlobals( array(
+ 'wgServer' => '//example.org',
+ 'wgScript' => '/w/index.php',
+ ) );
+ $ch = new CssContentHandler();
+ $content = $ch->makeRedirectContent( Title::newFromText( $title ) );
+ $this->assertInstanceOf( 'CssContent', $content );
+ $this->assertEquals( $expected, $content->serialize( CONTENT_FORMAT_CSS ) );
+ }
+
+ /**
+ * Keep this in sync with CssContentTest::provideGetRedirectTarget()
+ */
+ public static function provideMakeRedirectContent() {
+ return array(
+ array( 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ),
+ array( 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ),
+ array( 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+ );
+ }
+}
}
/**
- * Override this since CssContent does not support redirects yet
+ * @dataProvider provideGetRedirectTarget
+ */
+ public function testGetRedirectTarget( $title, $text ) {
+ $this->setMwGlobals( array(
+ 'wgServer' => '//example.org',
+ 'wgScriptPath' => '/w',
+ 'wgScript' => '/w/index.php',
+ ) );
+ $content = new CssContent( $text );
+ $target = $content->getRedirectTarget();
+ $this->assertEquals( $title, $target ? $target->getPrefixedText() : null );
+ }
+
+ /**
+ * Keep this in sync with CssContentHandlerTest::provideMakeRedirectContent()
*/
public static function provideGetRedirectTarget() {
return array(
- array( null, '' ),
+ array( 'MediaWiki:MonoBook.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=MediaWiki:MonoBook.css&action=raw&ctype=text/css);" ),
+ array( 'User:FooBar/common.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=User:FooBar/common.css&action=raw&ctype=text/css);" ),
+ array( 'Gadget:FooBaz.css', "/* #REDIRECT */@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+ # No #REDIRECT comment
+ array( null, "@import url(//example.org/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
+ # Wrong domain
+ array( null, "/* #REDIRECT */@import url(//example.com/w/index.php?title=Gadget:FooBaz.css&action=raw&ctype=text/css);" ),
);
}